package org.jeecg.modules.farming.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.farming.product.entity.FaProduct;
import org.jeecg.modules.farming.product.entity.ProductComment;
import org.jeecg.modules.farming.product.entity.ProductPraise;
import org.jeecg.modules.farming.product.service.IFaProductService;
import org.jeecg.modules.farming.product.service.IProductCommentService;
import org.jeecg.modules.farming.product.service.IProductPraiseService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.modules.system.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
* @Description: 供求产品表
* @Author: jeecg-boot
* @Date:   2020-12-13
* @Version: V1.0
*/
@Api(tags="供求产品表")
@RestController
@RequestMapping("/min/faProduct")
@Slf4j
public class MinProductController extends JeecgController<FaProduct, IFaProductService> {
   @Autowired
   private IFaProductService faProductService;
    @Autowired
    private IProductPraiseService praiseService;
    @Autowired
    private IProductCommentService commentService;
    @Autowired
    private ISysUserService userService;
    @Value("${wx.fullUrl}")
    private String fullUrl;
    /**
     * 分页列表查询
     *
     * @param faProduct
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "供求产品表-分页列表查询")
    @ApiOperation(value="供求产品表-分页列表查询", notes="供求产品表-分页列表查询")
    @GetMapping(value = "/min/list")
    public Result<?> queryPageMinList(FaProduct faProduct,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<FaProduct> queryWrapper = QueryGenerator.initQueryWrapper(faProduct, req.getParameterMap());
        Page<FaProduct> page = new Page<FaProduct>(pageNo, pageSize);
        IPage<FaProduct> pageList = faProductService.page(page, queryWrapper);
        List<FaProduct> records = pageList.getRecords();
        if(records!=null && records.size()>0){
            for(int i=0;i<records.size();i++){
                FaProduct product = records.get(i);
                LambdaQueryWrapper<ProductPraise> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(ProductPraise::getProductId,product.getId());
                //lambdaQueryWrapper.eq(ProductPraise::getOpenId,product.getOpenId());
                List<ProductPraise> list = praiseService.list(lambdaQueryWrapper);
                records.get(i).setIsPraise(0);
                if(list !=null && list.size()>0){
                    for(int n=0;n<list.size();n++){
                        if(list.get(n).getOpenId().equals(faProduct.getOpenId())){
                           records.get(i).setIsPraise(1);
                           break;
                       }
                   }
                }
                records.get(i).setPraiseCount((list == null && list.size() <=0) ? 0 : list.size());

                SysUser user = userService.getById(product.getUnionId());
                records.get(i).setHeadImgUrl(user.getAvatar());
                records.get(i).setUnionName(user.getRealname());

                LambdaQueryWrapper<ProductComment> commentWrapper = new LambdaQueryWrapper<>();
                commentWrapper.eq(ProductComment::getProductId,product.getId());
                //commentWrapper.eq(ProductComment::getOpenId,product.getOpenId());
                List<ProductComment> productCommentList = commentService.list(commentWrapper);
//显示多张图片
//                String[] split = product.getImgs().split(",");
//                records.get(i).setImgs(split[0]);
                records.get(i).setLmCount(productCommentList == null ? 0 : productCommentList.size());
                FaProduct n = records.get(i);
                if(n.getAttachment() != null)
                {
                    String[] split = n.getAttachment().split(",");
                    String strUrl = "";
                    for(int j=0;j<split.length;j++)
                    {
                        String url = split[j];
                        strUrl = SecurityUtil.GetFullUrl(url,fullUrl) + ",";
                    }
                    if(strUrl.length()>1)
                    {
                        strUrl = strUrl.substring(0,strUrl.length() - 1);
                    }
                    records.get(i).setAttachment(strUrl);
                }
                if(n.getImgs() !=null){
                    String[] split = n.getImgs().split(",");
                    String strUrl = "";
                    for(int j=0;j<split.length;j++)
                    {
                        String url = split[j];
                        strUrl = SecurityUtil.GetFullUrl(url,fullUrl) + ",";
                    }
                    if(strUrl.length()>1)
                    {
                        strUrl = strUrl.substring(0,strUrl.length() - 1);
                    }
                    records.get(i).setImgs(strUrl);
                }
                if(n.getHeadImgUrl() !=null){
                    String[] split = n.getHeadImgUrl().split(",");
                    String strUrl = "";
                    for(int j=0;j<split.length;j++)
                    {
                        String url = split[j];
                        strUrl = SecurityUtil.GetFullUrl(url,fullUrl) + ",";
                    }
                    if(strUrl.length()>1)
                    {
                        strUrl = strUrl.substring(0,strUrl.length() - 1);
                    }
                    records.get(i).setHeadImgUrl(strUrl);
                }
            }
            pageList.setRecords(records);
        }
        return Result.OK(pageList);
    }
   /**
    *   添加
    *
    * @param faProduct
    * @return
    */
   @AutoLog(value = "供求产品表-添加")
   @ApiOperation(value="供求产品表-添加", notes="供求产品表-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody FaProduct faProduct) {

       SysUser user = userService.getById(faProduct.getUnionId());
       faProduct.setPraiseCount(0);
       faProduct.setLmCount(0);
       faProduct.setUnionName(user.getUnionName());
       faProduct.setIsPraise(0);
       faProduct.setStatus(1);  //审核中
       faProduct.setCreateBy(user.getUsername());
       faProduct.setHeadImgUrl(user.getAvatar());
       faProductService.save(faProduct);
       return Result.OK("添加成功！");
   }

   /**
    *  编辑
    *
    * @param faProduct
    * @return
    */
   @AutoLog(value = "供求产品表-编辑")
   @ApiOperation(value="供求产品表-编辑", notes="供求产品表-编辑")
   @PostMapping(value = "/edit")
   public Result<?> edit(@RequestBody FaProduct faProduct) {
       faProductService.updateById(faProduct);
       return Result.OK("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "供求产品表-通过id删除")
   @ApiOperation(value="供求产品表-通过id删除", notes="供求产品表-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       faProductService.removeById(id);
       return Result.OK("删除成功!");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "供求产品表-批量删除")
   @ApiOperation(value="供求产品表-批量删除", notes="供求产品表-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.faProductService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.OK("批量删除成功!");
   }

    /**
     *  批量审核
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "供求产品表-批量审核")
    @ApiOperation(value="供求产品表-批量审核", notes="供求产品表-批量审核")
    @DeleteMapping(value = "/auditBatch")
    public Result<?> auditBatch(@RequestParam(name="ids",required=true) String ids,@RequestParam(name="auditValue",required=true) int auditValue) {
        this.faProductService.auditBatch(ids,auditValue);
        return Result.OK("批量审核成功!");
    }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @AutoLog(value = "供求产品表-通过id查询")
   @ApiOperation(value="供求产品表-通过id查询", notes="供求产品表-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id,@RequestParam(name="openId",required=true) String openId) {
       FaProduct faProduct = faProductService.getById(id);
       if(faProduct==null) {
           return Result.error("未找到对应数据");
       }
       ProductPraise praise = praiseService.getOneByOpenIdAndCommentId(openId,id);
       if(praise !=null){
           faProduct.setIsPraise(1);
       }
       //评论
       LambdaQueryWrapper<ProductPraise> lambdaQueryWrapper = new LambdaQueryWrapper<>();
       lambdaQueryWrapper.eq(ProductPraise::getProductId,faProduct.getId());
       List<ProductPraise> list = praiseService.list(lambdaQueryWrapper);
       faProduct.setLmCount(list == null ? 0 : list.size());
       if(null !=list && list.size()>0){
           for(int i=0;i<list.size();i++){
               if(list.get(i).getOpenId().equals(openId)){
                   faProduct.setIsPraise(1);
                   break;
               }
           }
       }
       LambdaQueryWrapper<ProductComment> productCommentLambdaQueryWrapper = new LambdaQueryWrapper<>();
       productCommentLambdaQueryWrapper.eq(ProductComment::getProductId,faProduct.getId());
       List<ProductComment> productComments = commentService.list(productCommentLambdaQueryWrapper);
       faProduct.setLmCount(productComments == null ? 0 : productComments.size());
       if(faProduct.getAttachment() != null)
       {
           String[] split = faProduct.getAttachment().split(",");
           String strUrl = "";
           for(int j=0;j<split.length;j++)
           {
               String url = split[j];
               strUrl = SecurityUtil.GetFullUrl(url,fullUrl) + ",";
           }
           if(strUrl.length()>1)
           {
               strUrl = strUrl.substring(0,strUrl.length() - 1);
           }
           faProduct.setAttachment(strUrl);
       }
       if(faProduct.getImgs() !=null){
           String[] split = faProduct.getImgs().split(",");
           String strUrl = "";
           for(int j=0;j<split.length;j++)
           {
               String url = split[j];
               strUrl = SecurityUtil.GetFullUrl(url,fullUrl) + ",";
           }
           if(strUrl.length()>1)
           {
               strUrl = strUrl.substring(0,strUrl.length() - 1);
           }
           faProduct.setImgs(strUrl);
       }
       if(faProduct.getHeadImgUrl() !=null){
           String[] split = faProduct.getHeadImgUrl().split(",");
           String strUrl = "";
           for(int j=0;j<split.length;j++)
           {
               String url = split[j];
               strUrl = SecurityUtil.GetFullUrl(url,fullUrl) + ",";
           }
           if(strUrl.length()>1)
           {
               strUrl = strUrl.substring(0,strUrl.length() - 1);
           }
           faProduct.setHeadImgUrl(strUrl);
       }
       return Result.OK(faProduct);
   }

   /**
   * 导出excel
   *
   * @param request
   * @param faProduct
   */
   @RequestMapping(value = "/exportXls")
   public ModelAndView exportXls(HttpServletRequest request, FaProduct faProduct) {
       return super.exportXls(request, faProduct, FaProduct.class, "供求产品表");
   }

   /**
     * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
   @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
       return super.importExcel(request, response, FaProduct.class);
   }

}
